//
//  GatherMonitor.m
//  LiveGather
//
//  Created by macintosh on 16/9/12.
//  Copyright © 2016年 macintosh. All rights reserved.
//

#import "GatherMonitor.h"
#import "MonitorInfo.h"

#define kLineSpacing 4
#define kInfoFontSize 13

@interface GatherMonitor ()
{
    BOOL _refreshResInfo;
    BOOL _refresSateInfo;
    BOOL _refresMonitorInfo;
    NSString *_stateStr;
    CGFloat _lineH;
}

@end

@implementation GatherMonitor

+ (instancetype)gatherMonitorWithGathorState:(GatherMonitorState)state
                                         res:(NSString *)res
                                 monitorInfo:(MonitorInfo *)monitorInfo
{
    GatherMonitor *gatherMonitor = [GatherMonitor new];
    gatherMonitor.state = state;
    gatherMonitor.resStr = res;
    gatherMonitor.monitorInfo = monitorInfo;
    return gatherMonitor;
}

- (instancetype)init
{
    if (self = [super init]) {
        UIFont *font = [UIFont systemFontOfSize:kInfoFontSize];
        _lineH = font.ascender - font.descender;
        CGFloat height = (_lineH + kLineSpacing) * 5;
        self.frame = CGRectMake(0, 0, 190, height);
        self.backgroundColor = [UIColor clearColor];
        _refreshResInfo = YES;
        _refresSateInfo = YES;
        _refresMonitorInfo = YES;
        _stateStr = @"----";
    }
    return self;
}

- (void)setState:(GatherMonitorState)state
{
    _state = state;
    
    _stateStr = [self stateStrWithState:_state];
    _refresSateInfo = YES;
    [self setNeedsDisplayInRect:CGRectMake(0, 0, self.frame.size.width, _lineH)];
}

- (void)setResStr:(NSString *)resStr
{
    if (!resStr) {
        resStr = @"---";
    }
    
    _resStr = resStr;
    _refreshResInfo = YES;
    [self setNeedsDisplayInRect:CGRectMake(0, (_lineH + kLineSpacing) * 1, self.frame.size.width, _lineH)];
}

- (void)setMonitorInfo:(MonitorInfo *)monitorInfo
{
    if (!monitorInfo) {
        monitorInfo = [MonitorInfo monitorInfo];
    }
    
    _monitorInfo = monitorInfo;
    _refresMonitorInfo = YES;
    [self setNeedsDisplayInRect:CGRectMake(0, (_lineH + kLineSpacing) * 2, self.frame.size.width, (_lineH + kLineSpacing) * 3)];
}

- (NSString *)stateStrWithState:(GatherMonitorState)sate
{
    NSString *sateStr = nil;
    switch (sate) {
        case GatherMonitorReady:
            sateStr = @"网络开始链接...";
            break;
        case GatherMonitorPending:
            sateStr = @"网络链接中...";
            break;
        case GatherMonitorStart:
            sateStr = @"网络已连接";
            break;
        case GatherMonitorStop:
            sateStr = @"网络已断开";
            break;
        case GatherMonitorError:
            sateStr = @"网络链接异常";
            break;
        case GatherMonitorUnknown:
            sateStr = @"-----";
            break;
    }
    return sateStr;
}

- (void)drawRect:(CGRect)rect
{
    NSDictionary *attrs = @{NSFontAttributeName : [UIFont systemFontOfSize:kInfoFontSize], NSForegroundColorAttributeName : [UIColor whiteColor]};
    
    if (_refresSateInfo) {
        _refresSateInfo = NO;
        
        [_stateStr drawInRect:CGRectMake(0, 0, self.frame.size.width, _lineH) withAttributes:[attrs copy]];
    }
    
    if (_refreshResInfo) {
        _refreshResInfo = NO;

        [[NSString stringWithFormat:@"分辨率:  %@", _resStr] drawInRect:CGRectMake(0, (_lineH + kLineSpacing) * 1, self.frame.size.width, _lineH) withAttributes:[attrs copy]];
    }
    
    if (_refresMonitorInfo) {
        _refresMonitorInfo = NO;

        [[NSString stringWithFormat:@"视频码率:  %@", _monitorInfo.vRate] drawInRect:CGRectMake(0, (_lineH + kLineSpacing) * 2, self.frame.size.width, _lineH) withAttributes:[attrs copy]];
        
        [[NSString stringWithFormat:@"音频码率:  %@", _monitorInfo.aRate] drawInRect:CGRectMake(0, (_lineH + kLineSpacing) * 3, self.frame.size.width, _lineH) withAttributes:[attrs copy]];
        
        [[NSString stringWithFormat:@"帧率:  %@", _monitorInfo.fps] drawInRect:CGRectMake(0, (_lineH + kLineSpacing) * 4, self.frame.size.width, _lineH) withAttributes:[attrs copy]];
    }
}

@end
